#include "protocolmessage.h"
#include "zhistringutil.h"

ProtocolMessage::ProtocolMessage(int category, QHash<QString, QString> data)
{
    this->category = category;
    this->data = data;
}

ProtocolMessage::ProtocolMessage(QString msg)
{
    QStringList list=decryptMessage(msg).split("|");
    if(ZhiStringUtil::str2int(list[0]) == ProtocolMessage::VALIDATA){
        this->category = ProtocolMessage::VALIDATA;
        for (int i = 1; i < list.length(); i+=2) {
            this->data.insert(list[i], list[i+1]);
        }
    }
}

ProtocolMessage::ProtocolMessage()
{
    this->category = ProtocolMessage::ILLEGAL;
    QHash<QString, QString> data;
    this->data = data;
}

QString ProtocolMessage::toMessage()
{
    QString _msg = ZhiStringUtil::int2str(this->category);
    QHash<QString, QString>::const_iterator iter = this->data.constBegin(); //遍历hash，方法1
    while(iter != this->data.constEnd())
    {
        _msg += "|" + iter.key() + "|" + iter.value();
        ++iter;
    }
    return this->encryptMessage(_msg);
}

QString ProtocolMessage::encryptMessage(QString msg)
{
    return msg;
}

QString ProtocolMessage::decryptMessage(QString msg)
{
    return msg;
}
